package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.dao.IMiguMusicRetentAddStatisticsDao;
import org.jsbd.boss.dao.IMiguMusicRetentAddUserDao;
import org.jsbd.boss.domian.MiguMusicRetentAddStatistics;
import org.jsbd.boss.domian.MiguMusicRetentAddUser;
import org.jsbd.boss.domian.MiguMusicSearchVo;
import org.jsbd.boss.service.IMiguMusicRetentAddStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.jsbd.boss.util.DateUtil;

@Service("miguMusicRetentAddStatisticsService")
public class MiguMusicRetentAddStatisticsServiceImpl implements IMiguMusicRetentAddStatisticsService {

	@Autowired
	private IMiguMusicRetentAddStatisticsDao miguMusicRetentAddStatisticsDao;

	@Autowired
	private IMiguMusicRetentAddUserDao miguMusicRetentAddUserDao;

	@Override
	public Map<String, Object> getListPage(DataGridModel page, MiguMusicRetentAddStatistics music) {
		
		Map<String, Object> results = new HashMap<String, Object>();

		if (music.getId() == null) {
			results.put("total", 0);
			results.put("rows", new ArrayList<>(1));
			return results;
		}
		if (music.getRetentMonth() == null || music.getRetentMonth().length() == 0) {
			music.setRetentMonth(DateUtil.getPreviousMonth());
		}
		if (music.getExecMonth() == null || music.getExecMonth().length() == 0) {
			music.setExecMonth(DateUtil.getCurrentMonth());
		}

		music.setPageInfo(page);

		List<MiguMusicRetentAddStatistics> list = miguMusicRetentAddStatisticsDao.getConfigExecTimesList(music);

		if (list == null || list.size() == 0) {
			results.put("total", 0);
			results.put("rows", new ArrayList<>(1));
			return results;
		}

		MiguMusicRetentAddStatistics object = list.get(0);
		music.setChannelId(object.getChannelId());
		music.setVersion(object.getVersion());
		music.setExecDate(DateUtil.getCurrentDateStr());

		//查询今天做的量
		List<MiguMusicRetentAddStatistics> doNumlist = miguMusicRetentAddStatisticsDao.getTodayDoNum(music);

		Integer step = 0;

		for (MiguMusicRetentAddStatistics statistics : list) {

			MiguMusicRetentAddUser query = new MiguMusicRetentAddUser();
			query.setChannelId(statistics.getChannelId());
			query.setVersion(statistics.getVersion());
			query.setCurrentMonth(music.getExecMonth());
			query.setExecTimes(statistics.getExecTimes());
			query.setRetentMonth(music.getRetentMonth());
			
			//查询现有留存补量用户数量
			Integer doNum = miguMusicRetentAddUserDao.getUserCountByExecTimes(query);

			step += doNum;

			statistics.setTotalNum(step);
			statistics.setDoNum(doNum);
			statistics.setExecMonth(music.getExecMonth());

			for (MiguMusicRetentAddStatistics addStatistics : doNumlist) {
				if (statistics.getExecTimes().intValue() == addStatistics.getExecTimes().intValue()) {
					//设置今天量
					statistics.setTodayNum(addStatistics.getDoNum());
					break;
				}
			}
			
			if(statistics.getExecTimes().intValue()==1){
				//查询总量
				MiguMusicRetentAddStatistics totalRow = new MiguMusicRetentAddStatistics();
				totalRow.setName(statistics.getName());
				totalRow.setChannelId(statistics.getChannelId());
				totalRow.setVersion(statistics.getVersion());
				totalRow.setRetentMonth(music.getRetentMonth());
				totalRow.setExecMonth(statistics.getExecMonth());
				totalRow.setExecTimes(0);
				Integer totalNum = miguMusicRetentAddStatisticsDao.getRetentUserCount(music);
				totalRow.setTotalNum(totalNum);
				totalRow.setDoNum(totalNum-doNum);
				list.add(totalRow);
				break;
			}
		}

		results.put("total", list.size());
		results.put("rows", list);

		return results;
	}

	@Override
	public Map<String, Object> getDayListPage(DataGridModel page, MiguMusicRetentAddStatistics music) {

		Map<String, Object> results = new HashMap<String, Object>();

		if (music.getId() == null && music.getExecDate() == null && music.getRetentMonth() == null) {
			results.put("total", 0);
			results.put("rows", new ArrayList<>(1));
		} else {
			music.setPageInfo(page);
			results.put("total", miguMusicRetentAddStatisticsDao.getDayListPageCount(music));
			List<MiguMusicRetentAddStatistics> list = (List<MiguMusicRetentAddStatistics>) miguMusicRetentAddStatisticsDao
					.getDayList(music);
			results.put("rows", list);
		}
		return results;
	}
	
	public static void main(String[] args) {
		System.out.println(DateUtil.getPreviousMonth());
	}

	@Override
	public List<MiguMusicRetentAddStatistics> getDayList(MiguMusicSearchVo miguMusicSearchVo) {
		return miguMusicRetentAddStatisticsDao.getMiguDayList(miguMusicSearchVo);
	}

}
